//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Event.h"
using namespace Cephei::QL;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::CEvent::CEvent (boost::shared_ptr<QuantLib::Event>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phEvent = NULL;
#endif
	_ppEvent = &childNative;
    
}
Cephei::QL::CEvent::CEvent (QuantLib::Event& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phEvent = NULL;
#endif
	_ppEvent = new boost::shared_ptr<QuantLib::Event> (&childNative);
    
    _EventOwner = owner;
    
}

Cephei::QL::CEvent::CEvent (CEvent^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phEvent = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppEvent = new boost::shared_ptr<QuantLib::Event> (copy->GetShared());
        
    }
}
Cephei::QL::CEvent::CEvent (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phEvent = NULL;
#endif
	if (!t->IsSubclassOf(CEvent::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CEvent::CEvent (QuantLib::Handle<QuantLib::Event>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phEvent = &childNative;
	_ppEvent = &static_cast<boost::shared_ptr<QuantLib::Event>>(childNative.currentLink());
    
    _EventOwner = owner;
}
Cephei::QL::CEvent::CEvent (QuantLib::Handle<QuantLib::Event> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phEvent = &childNative;
	_ppEvent = &static_cast<boost::shared_ptr<QuantLib::Event>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::CEvent::CEvent (QuantLib::Event childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phEvent = NULL;
#endif
	_ppEvent = new boost::shared_ptr<QuantLib::Event> (new QuantLib::Event (childNative));
    
}
#endif

Cephei::QL::CEvent::~CEvent ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppEvent != NULL)
    {
	    delete _ppEvent;
        _ppEvent = NULL;
    }
}
Cephei::QL::CEvent::!CEvent ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppEvent != NULL)
    {
	    delete _ppEvent;
    }
}
QuantLib::Event& Cephei::QL::CEvent::GetReference ()
{
    if (_ppEvent == NULL) throw gcnew NativeNullException ();
	return **_ppEvent;
}
boost::shared_ptr<QuantLib::Event>& Cephei::QL::CEvent::GetShared ()
{
    if (_ppEvent == NULL) throw gcnew NativeNullException ();
	return *_ppEvent;
}
QuantLib::Event* Cephei::QL::CEvent::GetPointer ()
{
    if (_ppEvent == NULL) throw gcnew NativeNullException ();
	return &**_ppEvent;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Event>& Cephei::QL::CEvent::GetHandle ()
{
	if (_phEvent == NULL)
	{
		_phEvent = new Handle<QuantLib::Event> (*_ppEvent);
	}
	return *_phEvent;
}
#endif
bool Cephei::QL::CEvent::HasNative () 
{
	return (_ppEvent != NULL);
}

Boolean Cephei::QL::CEvent::HasOccurred (Microsoft::FSharp::Core::FSharpOption<DateTime>^ refDate, Microsoft::FSharp::Core::FSharpOption<Boolean>^ includeRefDate)
{
    try
    {
        QuantLib::Date _refDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refDate) ? (QuantLib::Date)ValueHelper::Convert (refDate->Value) : QuantLib::Date()); //9a
        boost::optional<bool> _includeRefDate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (includeRefDate) ? boost::optional<bool> (ValueHelper::Convert (includeRefDate->Value)) : boost::optional<bool>()); //9
    	bool _rv = (bool)(*_ppEvent)->hasOccurred ( _refDate,  _includeRefDate );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::CEvent::Date::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppEvent)->date ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

